/******************************************************************
系统名称: 恒生电子手机银行3.0
类  名  称: HeaderMenu.java
软件版权: 恒生电子股份有限公司
系统版本: 1.0
开发人员: xisc
开发时间: 2014-7-25
功能说明: 头部布局
审核人员: 
相关文档: 
修改记录: 需求编号         修改日期         修改人员                     修改说明
 
******************************************************************/
package com.hundsun.fcp.widget;

import android.annotation.SuppressLint;
import android.os.Build;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hundsun.fcp.R;
import com.hundsun.fcp.common.utils.ViewUtils;

@SuppressLint("NewApi")
public class HeaderMenu {

	private View header ;
	/** 左按钮 */
	private ImageView ivLeft;
	/**右按钮 */
	private TextView tvRight;
	private LinearLayout ivLeftLayout,tvRightLayout;
	
	/** 中间标题 */
	private TextView tvTitle;
	
	/** 中间图片.标题和图片不能共存 */
//	private ImageView ivLogo;
	
//	private Activity mActivity;
	private Window mWindow;
	
	private OnClickListener mLeftClickListener;
	
	private OnClickListener mRightClickListener;
	
	public HeaderMenu(Window w, boolean img) {
		mWindow = w;
		initView();
//		toggleCenter(img);
	}
	
	
	/**
	 * 默认的中间区域显示文字
	 * @param activity
	 */
	public HeaderMenu(Window w) {
		this(w, false);
	}
	
	private void initView() {
		header = findViewById(R.id.main_header) ;
		ivLeft = (ImageView) findViewById(R.id.iv_left);
		ivLeftLayout = (LinearLayout)findViewById(R.id.iv_left_layout);
		tvRightLayout = (LinearLayout)findViewById(R.id.iv_right_layout);
		tvRight = (TextView) findViewById(R.id.tv_right);
		tvTitle = (TextView) findViewById(R.id.tv_title);
//		ivLogo = (ImageView) findViewById(R.id.iv_logo);
	}
	
	/**
	 * 设置左边按钮监听
	 * @param leftClickListener
	 */
	public void setLeftClickListener(OnClickListener leftClickListener) {
		ivLeftLayout.setOnClickListener(leftClickListener);
	}
	
	/**
	 * 设置背景
	 * @param resId
	 */
	public void setLeftSrc(int resId){
		ivLeft.setImageResource(resId);
	}

	/**
	 * 设置右边按钮监听
	 * @param rightClickListener
	 */
	public void setRightClickListener(OnClickListener rightClickListener) {
		tvRightLayout.setOnClickListener(rightClickListener);
	}

	/**
	 * 设置背景
	 * @param resId
	 */
	public void setRightSrc(int resId){
		tvRight.setBackgroundResource(resId);
	}
	
	/**
	 * 隐藏左边按钮 
	 */
	public void toggleLeft(int visibility) {
		ivLeft.setVisibility(visibility);
	}
	
	/**
	 * 隐藏右边按钮
	 */
	public void toggleRight(int visibility) {
		tvRight.setVisibility(visibility);
	}
	
	/**
	 * 中间区域:文字和图片的切换
	 *//*
	public void toggleCenter(boolean img) {
		ivLogo.setVisibility(img ? View.VISIBLE : View.GONE);
		tvTitle.setVisibility(!img ? View.VISIBLE : View.GONE);
	}*/
	
	/**
	 * 设置中间标题
	 * @param title
	 */
	public void setTitle(String title) {
		if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN ) {
			tvTitle.setBackground(null);
		}
		else {
			tvTitle.setBackgroundDrawable(null);
		}
		ViewUtils.setText(tvTitle, title);
	}
	
	public String getTitle(){
		return tvTitle.getText().toString() ;
	}
	
	/**
	 * 通过resource id设置中间标题
	 * @param resid
	 */
	public void setTitle(int resid) {
		if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN ) {
			tvTitle.setBackground(null);
		}
		else {
			tvTitle.setBackgroundDrawable(null);
		}
		tvTitle.setText(resid);
	}

	/**
	 * 设置左边图标
	 * @param resId
	 */
	public void setLeftBG(int resId) {
		ivLeft.setImageResource(resId);
	}
	
	/**
	 * 设置右边图标
	 * @param resId
	 */
	public void setRightBG(int resId) {
		tvRight.setText("") ;
		tvRight.setBackgroundResource(resId);
	}
	
	/**
	 * 设置右边文字
	 * @param resId
	 */
	public void setRightText(int resId) {
		if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN ) {
			tvRight.setBackground(null);
		}
		else {
			tvRight.setBackgroundDrawable(null);
		}
		tvRight.setText(resId);
	}
	
	
	/**
	 * 设置中间图标
	 * @param resId
	 */
	public void setLogo(int resId) {
		tvTitle.setText("");
		tvTitle.setBackgroundResource(resId);
//		ivLogo.setImageResource(resId);
	}
	/**
	 * 设置标题是否隐藏
	 * @param isShow
	 */
	public void showHeader(boolean isShow){
		header.setVisibility(isShow?View.VISIBLE:View.GONE) ;
	}
	
	private View findViewById(int id) {
		return mWindow.findViewById(id); 
	}
}
